package judlawei.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.io.File;

import judlawei.readfile.Reader;

public class Buscador extends JPanel {
    
    private File arquivo = null;
    
    private JButton botaoBuscar;
    private JTextField campoNomeArq;
    private JLabel rotNomeArq;

    public Buscador() {

        botaoBuscar = new JButton();
        rotNomeArq = new JLabel();
        campoNomeArq = new JTextField();
        botaoBuscar.setText("Buscar");

        rotNomeArq.setText("Nome do Arquivo:");

        campoNomeArq.setText("inserir nome");


        construirLayout();
        initBotaoBuscar();
        
    }

    
    private void construirLayout(){
        javax.swing.GroupLayout buscadorLayout = new javax.swing.GroupLayout(this);
        this.setLayout(buscadorLayout);
        buscadorLayout.setHorizontalGroup(
            buscadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buscadorLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rotNomeArq)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addComponent(campoNomeArq, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoBuscar)
                .addGap(33, 33, 33))
        );
        buscadorLayout.setVerticalGroup(
            buscadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buscadorLayout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(buscadorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNomeArq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rotNomeArq)
                    .addComponent(botaoBuscar))
                .addContainerGap(30, Short.MAX_VALUE))
        );
    }
    
    private void initBotaoBuscar(){
        
        botaoBuscar.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                buscar();
            }
        });
               
    }
    
    public void buscar(){
       
        JFileChooser fileChooser = new JFileChooser();
        
        fileChooser.setCurrentDirectory(arquivo);
       
        int optFC = fileChooser.showOpenDialog(this);
        if (optFC == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            arquivo = file;  
            campoNomeArq.setText(arquivo.getAbsolutePath());
        }
               
    }
    
    
    public File getArquivo(){
        return arquivo;
    }

    
    
}
